<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>设备管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../js/lib/layui/dist/css/layui.css">
</head>
<body>
<div style="padding: 15px;" class="usereditbody layui-form-pane">
    <h1>设备管理</h1>
    <form class="layui-form" action="">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="color:green;">设备信息维护</legend>
        </fieldset>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>机型信息</legend>
                <div id="packages_departmentnetworkone" style="margin: 10px">
                    <!-- <input type="checkbox" id="versioninfo" name="1001" title="联系人com.android.contacts"
                            value="com.android.contacts"> -->
                </div>
                <div class="layui-inline" style="margin-left: 20px">
                    <label class="layui-form-label">新增机型:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="newdevice" id="newdevice" autocomplete="off"
                                class="layui-input">
                    </div>
                </div>
            </fieldset>
        </div>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>版本信息</legend>
                <div id="packages_departmentnetworkone" style="margin: 10px">
                    <!-- <input type="checkbox" id="versioninfo" name="1001" title="联系人com.android.contacts"
                            value="com.android.contacts"> -->
                </div>
                <div class="layui-inline" style="margin-left: 20px">
                    <label class="layui-form-label">新增版本:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="newversion" id="newversion" autocomplete="off"
                                class="layui-input">
                    </div>
                </div>
            </fieldset>
        </div>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>区域信息</legend>
                <div id="packages_departmentnetworkone" style="margin: 10px">
                    <!-- 包名展示  -->
                    <!-- <input type="checkbox" id="pkgindevice" name="1001" title="联系人com.android.contacts"
                            value="com.android.contacts"> -->
                </div>
                <div class="layui-inline"  style="margin-left: 20px">
                    <label class="layui-form-label">新增区域:</label>
                    <div class="layui-input-inline">
                        <input type="text" name="newarea" id="newarea" autocomplete="off"
                                class="layui-input">
                    </div>
                </div>
            </fieldset>
        </div>
    </form>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" id="submit" data-type="commit" lay-filter="demo1"
                    style="width: 200px">新增
            </button>
            <!--<button type="reset" data-type="editreset" class="layui-btn layui-btn-primary" id="editreset">重置</button>-->
        </div>
    </div>
</div>
<script src="../js/lib/jquery/dist/jquery.min.js"></script>
<script src="../js/lib/PACE/pace.min.js"></script>
<script src="../js/lib/lodash/dist/lodash.min.js"></script>
<script src="../js/lib/layui/dist/layui.js"></script>

<script>
        $().ready(function () {
            
        });

        // baseurl = "http://10.234.197.58:8080"
        baseurl = "http://0.0.0.0:8080"
        
        // let index = parent.layer.getFrameIndex(window.name);  //获取子窗口索引
        function get_devcies(){
            $.ajax({
                url: baseurl + "/monkey/getdevices",
                type: 'get',
                dataType: 'json',
                async: false,
                // xhrFields: {
                //     withCredentials: true
                // },
                crossDomain: true,
                success: function (res) {
                    console.log(res);
                    var state = new Array();
                    res.devices.forEach(function(currentValue, index, arr){
                        console.log('forEach')
                        $('#device').append('<option value="' + currentValue.sn + '">' + currentValue.product + '</option>');
                        state.push(currentValue.product.split("_")[1]);
                        // console.log(currentValue.product);
                        // $('#submit').attr("disabled","disabled")
                    });
                    console.log(state);
                    $('#submit').attr("disabled",true);
                    $('#submit').addClass("layui-btn-disabled");
                    state.forEach(function(value, index, arr){
                        if(value == "idle"){
                            $('#submit').attr("disabled",false);
                            $('#submit').removeClass("layui-btn-disabled");
                        }
                    });
                    // if(currentValue.product.split("_")[1] == "BUSY"){
                    //         $('#submit').attr("disabled","disabled")
                    // }
                }
            });
        }

        get_devcies();

        //JavaScript代码区域
        layui.use(['form', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element'], function () {

            var form = layui.form;

            // form.render();

            var $ = layui.$;

            // applystate.forEach(function(currentValue, index, arr){
            //     $('#status').append('<option value="' + currentValue.id + '">' + currentValue.desc + '</option>');
            // });

            var active = {
                commit: function () {
                    var errhandler = ""; 
                    $("input:checkbox[id='error']:checked").each(function(i){
                        errhandler += $(this).val() + ";";
                    });
                    var pkg = "";
                    $("input:checkbox[id='pkgindevice']:checked").each(function(i){
                        pkg += $(this).val() + ";";
                    });
                    let build = 0;
                    $("input:checkbox[id='build']:checked").each(function(i){
                        build = 1;
                    });
                    let monkey = 0;
                    $("input:checkbox[id='monkey']:checked").each(function(i){
                        monkey = 1;
                    });
                    // var packages_departmentnetworkone = "";
                    // $("input:checkbox[id='packages_departmentnetworkone']:checked").each(function(i){
                    //     packages_departmentnetworkone += $(this).val() + ";";
                    // });
                    let param = {
                        'build': build,
                        'monkey': monkey,
                        'device': $('#device').val(),
                        'version': $('#version').val() == "" ? "o-dev" :$('#version').val(),
                        'area': $('#version').val() == "" ? "cn" :$('#version').val(),
                        // 'packages_departmentnetworkone': packages_departmentnetworkone,
                        'pkg': pkg,
                        'package': $('#package').val(),
                        'loglevel': $('#loglevel').val(),
                        'seed': $('#seed').val() == "" ? "1000" :$('#version').val(),
                        'throttle': $('#throttle').val() == "" ? "500" :$('#version').val(),
                        'eventsnumber': $('#eventsnumber').val() == "" ? "100000" :$('#version').val(),
                        'pct_touch': $('#pct-touch').val() == "" ? "30" :$('#pct-touch').val(),
                        'pct_motion': $('#pct-motion').val() == "" ? "20" :$('#pct-motion').val(),
                        'pct_trackball': $('#pct-trackball').val() == "" ? "0" :$('#pct-trackball').val(),
                        'pct_nav': $('#pct-nav').val() == "" ? "20" :$('#pct-nav').val(),               
                        'pct_majornav': $('#pct-majornav').val() == "" ? "15" :$('#pct-majornav').val(),
                        'pct_syskeys': $('#pct-syskeys').val() == "" ? "0" :$('#pct-syskeys').val(),
                        'pct_appswitch': $('#pct-appswitch').val() == "" ? "5" :$('#pct-appswitch').val(), 
                        'pct_anyevent': $('#pct-anyevent').val() == "" ? "10" :$('#pct-anyevent').val(),
                        'errhandler': errhandler,
                    };
                    console.log(param);
                    $.ajax({
                        url: baseurl + "/monkey/start",
                        type: 'post',
                        dataType: 'json',
                        data: JSON.stringify(param),
                        async: false,
                        // xhrFields: {
                        //     withCredentials: true
                        // },
                        crossDomain: true,
                        success: function (res) {
                            $('#submit').attr("disabled",true);
                            $('#submit').addClass("layui-btn-disabled");
                        }
                    });
                }
            };

            $('#submit').on('click', function () {
                console.log("commit");
                var type = $(this).data('type');
                console.log(type);
                active[type] ? active[type].call(this) : '';
            });

            form.on('select(device)', function(data){
                console.log("device select");
                console.log(data.value);
                $.ajax({
                url: baseurl + "/monkey/getpackagesindevice?device=" + data.value,
                type: 'get',
                dataType: 'json',
                async: false,
                // xhrFields: {
                //     withCredentials: true
                // },
                crossDomain: true,
                success: function (res) {
                    $("#packages").html("");
                    console.log(res);
                    $.each(res.packages, function(key, val) {
                        if(val.value != " usage"){
                            var input = '<input type="checkbox" id="pkgindevice" name="'+ val.key+'" title="'+ val.value+'" value="'+ val.value + '" >'
                            console.log(input);
                            $("#packages").append(input);
                        }
                    }); 
                    form.render();
                }
            });
            });
        });
</script>
</body>
</html>
